package com.qf.controller;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.qf.entity.DTO.MeetingDTO;
import com.qf.entity.po.MeetingPub;
import com.qf.entity.po.MeetingType;
import com.qf.service.MeetingService;
import com.qf.util.RedisRepeatUtil;
import com.qf.util.ResponseData;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @version : 1.0
 * @author:宋煜
 * @date: 2022/5/27 19:41
 * 会议页面增删改查实现
 */
@Controller
@RequestMapping("meeting")
public class MeetingController {

    @Autowired
    private MeetingService meetingService;
    @Autowired
    private RedisRepeatUtil repeatUtil;
    /**
     * TODO 跳转到会议页面  /meeting/toMeetingPage
     */
    @RequestMapping("toMeetingPage")
    public String toMeetingPage(Model model,MeetingDTO meetingDTO){
        model.addAttribute("meetingDTO",meetingDTO);
        model.addAttribute("list",meetingService.getMeetingList());
        return "meeting";
    }
    /**
     * TODO 跳转到会议增加页面 /meeting/meeting-add
     */
    @RequestMapping("meeting-add")
    public String toMeetingAddPage(Model model){
        //解决幂等性uuid
        //key的命名：token:meeting:meeting-add:uuid
        model.addAttribute("uuid",repeatUtil.generateToken(RedisRepeatUtil.TOKEN_MEETING_ADD));
        //会议id
        model.addAttribute("UUid",getUUid());
        model.addAttribute("typeList",meetingService.getTypeList());
        return "meeting-add";
    }
    /**
     * TODO 新增会议跳转到主页 /meeting/addMeeting
     */
    @PostMapping("addMeeting")
    @ResponseBody
    public ResponseData<T> addMeeting(MeetingPub meetingPub, HttpServletRequest request){
        //幂等性处理的uuid
        String uuid = request.getParameter("uuid");
        String key = RedisRepeatUtil.TOKEN_MEETING_ADD+uuid;
        boolean b = repeatUtil.compareToken(key, uuid);
        if (!b){
            return new ResponseData<T>("400","数据异常或超时,请重新操作");
        }
        meetingPub.setPcode(meetingService.getPCode(meetingPub.getPtime()));
        meetingService.insertMeeting(meetingPub);
        return new ResponseData<T>("200","新增成功");
    }
    /**
     * TODO 删除单个 /meeting/delMeeting
     */
    @PostMapping("delMeeting")
    @ResponseBody
    public ResponseData<T> delMeeting(String id){
        meetingService.delMeeting(id);
        return new ResponseData<T>("200","删除成功");
    }
    /**
     * TODO 生成uuid
     */
    public String getUUid(){
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
    /**
     * TODO 批量删除
     */
    @ResponseBody
    @PostMapping("deleteBatch")
    public ResponseData deleteBatch(@RequestParam("ids")String idsStr){
        String[] ids = idsStr.split(",");
        meetingService.deleteBatch(ids);
        return new ResponseData("200","删除成功");
    }
    /**
     * TODO 编辑逻辑  /meeting/edit
     */
    @RequestMapping("edit")
    public String toEditPage(String id, Model model){
        model.addAttribute("typeList",meetingService.getTypeList());
        MeetingPub meetingPub = meetingService.selectById(id);
        meetingPub.setPtime(meetingPub.getPtime().replaceAll(" ","T"));
        model.addAttribute("meetingPub",meetingPub);
        return "edit";
    }
    /**
     * TODO 更新逻辑 /meeting/updateMeeting
     */
    @RequestMapping("updateMeeting")
    @ResponseBody
    public ResponseData updateMeeting(MeetingPub meetingPub){
        meetingService.updateMeeting(meetingPub);
        return new ResponseData("200","更新成功");
    }
    /**
     * TODO 更新状态 /meeting/updateDown
     */
    @RequestMapping("updateDown")
    @ResponseBody
    public ResponseData updateStatusDown(String id){
        meetingService.updateStatusDown(id);
        return new ResponseData("200","更新成功");
    }
    /**
     * TODO 更新状态 /meeting/updateUp
     */
    @RequestMapping("updateUp")
    @ResponseBody
    public ResponseData updateStatusUp(String id){
        meetingService.updateStatusUp(id);
        return new ResponseData("200","更新成功");
    }
    /**
     * TODO 列表条件查询  /meeting/listCondition
     */
    @RequestMapping("listCondition")
    public String listCondition(Model model, MeetingDTO meetingDTO){
        model.addAttribute("meetingDTO",meetingDTO);
        model.addAttribute("list",meetingService.listCondition(meetingDTO));
        return "meeting";
    }
    /**
     * TODO 跳转到表格插入界面 /meeting/toImportExcel
     */
    @RequestMapping("toImportExcel")
    public String toImportExcel(Model model){
        //解决幂等性uuid
        //key的命名：token:meeting:meeting-add:uuid
        model.addAttribute("uuid",repeatUtil.generateToken(RedisRepeatUtil.TOKEN_MEETING_EXCEL));
        return "importExcel";
    }
    /**
     * TODO 接收前端提交的表格文件 插入数据库 /meeting/importExcel
     */
    @PostMapping("importExcel")
    @ResponseBody
    public ResponseData<T> importExcel(@RequestParam("file") MultipartFile file,HttpServletRequest request) throws Exception {
        //幂等性处理的uuid
        String uuid = request.getParameter("uuid");
        String key = RedisRepeatUtil.TOKEN_MEETING_EXCEL+uuid;
        boolean b = repeatUtil.compareToken(key, uuid);
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        List<MeetingType> list = ExcelImportUtil.importExcel(file.getInputStream(), MeetingType.class, params);
        List<String> listDBTName = meetingService.getAllTName();
        list = this.repeat(listDBTName,list);//去重
        if(list.size()==0){
            return new ResponseData<T>("402","0");
        }
        if (!b){
            return new ResponseData<T>("400","数据异常或超时,请重新操作");
        }
        return new ResponseData<T>("200",String.valueOf(meetingService.insertType(list)));
    }
    /**
     * TODO 重复去重方法
     */
    public List<MeetingType> repeat(List<String> listDBTName,List<MeetingType> listTName){

        for (int i = 0; i <  listTName.size(); i++) {
            if(listDBTName.contains( listTName.get(i).getTname())){
                listTName.remove( listTName.get(i));
                i--;
            }
        }
        return  listTName;
    }
    /**
     * TODO 下载会议表 /meeting/downloadExcel
     */
    @RequestMapping("downloadExcel")
    @ResponseBody
    public ResponseData downloadExcel(MeetingDTO meetingDTO) throws Exception {
        meetingService.downloadExcel(meetingDTO);
        return new ResponseData("200","下载成功!文件路径D:\\downloadExcel\\会议发单表.xls");
    }
}
